# -*- coding = utf-8 -*-
# @Time : 2021/8/9 19:11
# @Author : 龙王赘婿_彪少
# @File : meizi2.py.py
# @Software: PyCharm

# 爬取妹子图片

#正则表达式.Beautiful Soup.Lxml都是解析网页用的
import urllib.request, urllib.error
from lxml import html
from bs4 import BeautifulSoup  # 网页解析
import os
import re


def header():
    headers = {
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'zh-CN,zh;q=0.9',
        'cache-control': 'max-age=0',
        'cookie': 'Hm_lvt_cb7f29be3c304cd3bb0c65a4faa96c30=1626761071; Hm_lpvt_cb7f29be3c304cd3bb0c65a4faa96c30=1627519587',
        'if-modified-since': 'Wed, 28 Jul 2021 16:29:48 GMT',
        'sec-fetch-dest': 'document',
        'sec-fetch-mode': 'navigate',
        'sec-fetch-site': 'none',
        'sec-fetch-user': '?1',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
    }
    return headers

'''
函数名：    connetion
入口参数：   url 链接网络字符串
返回值：    content
功能：     
'''
def getHtml(url):
    headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36", }
    request = urllib.request.Request(url=url, headers=headers, method="GET")
    html = ""
    try:
        resource = urllib.request.urlopen(request)
        html = resource.read().decode("utf-8")
        print(html)
    except urllib.error.URLError as e:
        if hasattr(e, "code"):
            print(e.code)
        if hasattr(e, "reason"):
            print(e.reason)
    return html


if __name__ == '__main__':
    # url = 'https://www.enterdesk.com/special/meizi/meng/'
    url = "https://mm.enterdesk.com/"
    # file = open("./pptok.html", mode="w")
    # file.write(getHtml(url))
    # file.close
    html = getHtml(url)
    soup = BeautifulSoup(html, "lxml")

    # 打印所有
    print(soup.prettify())

    path = '../../download/'
    i = 0
    pat = re.compile(".jpg")
    for img in soup.find_all('img'):
        if (pat.search(img.get('src'))):
            print(img.get('src'))
            urllib.request.urlretrieve(img.get('src'), path + str(img.get('title')) + ".jpg")
            i = i + 1

    print("end")
